package com.serviceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.TableHistoryMapper;
import com.mapper.UsersMapper;
import com.mapper.WtdMapper;
import com.pojo.Wtd;
import com.service.WtdService;

@Service(value = "wtdService")
public class WtdServiceImpl implements WtdService{
	@Resource
	private WtdMapper wtdMapper;
	@Resource
	private UsersMapper usersMapper;
	//2017.12.9 9:11 yy
	@Resource
	private TableHistoryMapper tableHistoryMapper;

	public WtdServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	//撤单时,卖方需要返还订单剩余的商品数量
	//2017.12.18 11:11 yy 添加事务处理
	@Override
	public void updatecxwtd(Map<String, Object> para) {
		// TODO Auto-generated method stub
		String type=(String)para.get("ttype") ;
		if(type.equals("卖")) {			
			int wc=(int)para.get("wcount");
			int tc=(int)para.get("tcount");
			String uid=(String)para.get("UserID");
			String goods=(String)para.get("goods");
			int wt=wc-tc;
			HashMap<String, Object> tests = new HashMap<String, Object>();
			tests.put("djgoodnum",wt);
			tests.put("UserID",uid);
			tests.put("goods",goods);
			wtdMapper.updatewtds(tests);
			long wid=(long)para.get("wid");
			//将要撤销的wtd插入到wtd_his表中,将状态置为1 2017.12.8 15:33 yy
			yscwtd(wid);
			wtdMapper.deletewtds(wid);
		}
		else {
			//将要撤销的wtd插入到wtd_his表中,将状态置为1 2017.12.8 15:33 yy
			yscwtd((long)para.get("wid"));
			wtdMapper.deletewtds((long)para.get("wid"));
		}
		
	}
	
	//将要撤销的wtd插入到wtd_his表中,将状态置为1 2017.12.8 15:33 yy
		public void yscwtd(long wid) {
			Wtd wh = tableHistoryMapper.selectwtdHistory(wid);
			HashMap<String, Object> wh1 = new HashMap<String, Object>();
			wh1.put("UserID", wh.getUserID());
			wh1.put("ttype", wh.getTtype());
			wh1.put("goods", wh.getGoods());
			wh1.put("wcount", wh.getWcount());
			wh1.put("wprice", wh.getWprice());
			wh1.put("consume", wh.getConsume());
			wh1.put("wtime", wh.getWtime());
			wh1.put("tcount", wh.getTcount());
			wh1.put("state", 1);
			tableHistoryMapper.insertwtdHistory(wh1);
		}
	
	//晚上9:30  清除委托单,买方直接清,卖方需要更改用户信息中的商品数量
		@Override
		public void qcwtd() {
			// TODO Auto-generated method stub
			List l = wtdMapper.selectAllwtd();
			System.out.println(l);
			//for (int i = 0; i < l.size(); i++) {
			while (l.size()>0) {
				// 获得wtd表中的wcount tcount
				Wtd w = (Wtd) l.get(0);
				int wc = w.getWcount();
				int tc =w.getTcount();
				int sb = wc - tc;
				String type = w.getTtype();
				String goods=w.getGoods();
				long wid = w.getWid(); 
				updatedeletewtd(w.getUserID(),wid,type,sb,goods);
				l.remove(0);
			}
		}
		//2017.12.18 11:11 yy 添加事务处理
		@Override
		public void updatedeletewtd(String uid,long wid,String type,int sb,String goods) {
				if (type.equals("卖")) {
					System.out.println("66:"+type+","+wid);
					HashMap<String, Object> tests = new HashMap<String, Object>();
					tests.put("UserID", uid);
					tests.put("djgoodnum", sb);
					tests.put("goods",goods);
					wtdMapper.updatewtds(tests);
					wtdMapper.deletewtds(wid);
					wtdMapper.deletesstatis();
				}
				else {
					System.out.println("74:"+type+","+wid);
					wtdMapper.deletewtds(wid);
					wtdMapper.deletesstatis();		
				}
		}

		@Override
		public void buding() {
			// TODO Auto-generated method stub
			usersMapper.excerefor();
			usersMapper.excerefor2();
		}
		

}
